package com.stox.ui.javafx.core.widget.wrapper;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.Node;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

@Component
public class ToolBar extends javafx.scene.control.ToolBar  implements InitializingBean {

	public static interface ToolBarChild extends Ordered{}
	
	@Autowired
	private ApplicationContext applicationContext;
	
	public ToolBar() {
		super();
	}

	public ToolBar(Node... arg0) {
		super(arg0);
	}
	
	@Override
	public void afterPropertiesSet() throws Exception {
		List<ToolBarChild> toolBarChildren = new ArrayList<ToolBarChild>(applicationContext.getBeansOfType(ToolBarChild.class).values());
		OrderComparator.sort(toolBarChildren);
		List<Node> nodes = new ArrayList<Node>(toolBarChildren.size());
		for(ToolBarChild toolBarChild : toolBarChildren) {
			if(toolBarChild instanceof Node) {
				nodes.add((Node)toolBarChild);
			}
		}
		getItems().addAll(nodes);
	}

}
